
############ get the data from each file for background ########################

setwd ("C:/pyrogeography/FIRESCARASCII/outdir/")
weather.dir = ("G:/weather/")
data.dir = ("C:/pyrogeography/FIRESCARASCII/outdir/")

###### read in the base file #

bkgd = read.csv ("fire00_09.csv")
bkgd [1:5,]

#bkgd$LATDEC = sprintf('%.2f',bkgd$LATDEC); bkgd$LONGDEC = sprintf('%.2f',bkgd$LONGDEC); 
bkgd$month = sprintf('%02d',bkgd$month)
bkgd$tfile = paste(bkgd$year,bkgd$month,sep=''); bkgd = bkgd[order(bkgd$tfile),]
bkgd$X = NULL
bkgd$species = NULL
#bkgd$month = NULL
#bkgd$year = NULL
#bkgd$tfile = NULL
names(bkgd)=c("LATDEC","LONGDEC","month","year","tfile")
bkgd = bkgd[which(bkgd$LONGDEC>0 & bkgd$LATDEC<0),]  
write.csv (bkgd, "bkgd.csv")

#set up the bkgd file as a data.frame with only unique lat and lon
occur.file = bkgd
dim (occur.file)

occur.lon = occur.file$LONGDEC
occur.lat = occur.file$LATDEC
occur.file = data.frame(occur.lon, occur.lat); names (occur.file) = c("LONGDEC", "LATDEC")
occur.file [1:2,]
occur.file = occur.file[which(occur.file$LONGDEC>0 & occur.file$LATDEC<0),]  
bkgd = unique (occur.file)
write.csv(bkgd, "occur.csv")
dim(bkgd)
length (bkgd)

######### set up for extracting background from each month year combo  ############

weather = list.files(weather.dir, pattern = "200***"); print (weather)  # get list of files in weather.dir
 

############################################################### 
# read in the data files and append to one mega table     #####
##############################################################
for (test in (weather)){
	if (file.exists(paste(weather.dir,test,'/data.csv',sep=''))) {
		cat(test,'\n')  }
		#read in the csv.data files and append
		date = c()
    for (x in c(paste(weather.dir,weather,'/data.csv',sep=''),as.is=T)) {
      u<-read.csv(x)
      u$dataset = x
      date <- rbind(date, u)
      }
      }

#limit table to match the unique background

dim(date)
date[1:2,]
bkgd[1:2,]
         
test = unique (date$dataset); test # see if all the files got dragged in

#make matching column 
date$latlon = paste(date$lat,date$lon,sep='')
bkgd$latlon = paste(bkgd$LATDEC, bkgd$LONGDEC, sep='')

testt = merge (date, bkgd)
write.csv (testt, "testt.csv")

dim(bkgd) # testt should be double if all match
dim(testt)
